package com.gs.study.web;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import com.gs.study.util.BaseController;

import freemarker.template.Template;

/** 
 * @author gaoshang 
 * @date 创建时间：2018年5月15日 下午4:23:54 
 * @version 1.0 
 * @parameter  
 * @since  
 * @return  
 */
@RequestMapping(value="/email")
@Controller
public class EmailController extends BaseController{
	//private Logger logger = Logger.getLogger(getClass());
	 
	@Autowired
	private JavaMailSender mailSender;
    @Autowired
    private TemplateEngine templateEngine;
    @Autowired
    private FreeMarkerConfigurer configurer;

	/**
	 * 邮件
	 * @throws Exception
	 */
	@RequestMapping(value="/1")
	public void sendSimpleMail() throws Exception {

		SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom("897012340@qq.com");
		message.setTo("897012340@qq.com");
		message.setSubject("主题：简单邮件");
		message.setText("测试邮件内容");
		mailSender.send(message);
	}
	
	
	/**
	 * 附件
	 * @throws Exception
	 */
	@RequestMapping(value="/2")
	public void sendAttachmentsMail() throws Exception {

		MimeMessage mimeMessage = mailSender.createMimeMessage();

		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
		helper.setFrom("897012340@qq.com");
		helper.setTo("897012340@qq.com");
		helper.setSubject("主题：有附件");
		helper.setText("有附件的邮件");

		FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/weixin.jpg"));
		helper.addAttachment("附件-1.jpg", file);
		helper.addAttachment("附件-2.jpg", file);

		mailSender.send(mimeMessage);
	}

	/**
	 * 静态资源
	 * @throws Exception
	 */
	@RequestMapping(value="/3")
	public void sendInlineMail() throws Exception {

		MimeMessage mimeMessage = mailSender.createMimeMessage();

		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
		helper.setFrom("897012340@qq.com");
		helper.setTo("897012340@qq.com");
		helper.setSubject("主题：嵌入静态资源");
		helper.setText("<html><body><img src=\"cid:weixin\" ></body></html>", true);

		FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/weixin.jpg"));
		helper.addInline("weixin", file);

		mailSender.send(mimeMessage);
	}

	/**
	 * 模板
	 * @throws Exception
	 */
	@RequestMapping(value="/4")
	public void sendThymeleafTemplateMail() throws Exception {

		MimeMessage mimeMessage = mailSender.createMimeMessage();
        Context context = new Context();
        context.setVariable("host", "公司模板");
        String process = templateEngine.process("thymeleaf", context);
		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
		helper.setFrom("897012340@qq.com");
		helper.setTo("897012340@qq.com");
		helper.setSubject("主题：模板邮件");
		helper.setText(process, true);
		mailSender.send(mimeMessage);
	}
	
	
	/**
	 * 模板
	 * @throws Exception
	 */
	@RequestMapping(value="/5")
	public void sendFreeMarkerTemplateMail() throws Exception {
		MimeMessage mimeMessage = mailSender.createMimeMessage();
        Map<String, Object> model = new HashMap<>();
        model.put("host", "模板");
   	 	Template template = configurer.getConfiguration().getTemplate("freemarker.ftl");
   	 	String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
		helper.setFrom("897012340@qq.com");
		helper.setTo("897012340@qq.com");
		helper.setSubject("主题：模板邮件");
		helper.setText(text, true);
		mailSender.send(mimeMessage);
	}
	
}
